package cn.usian.service.serviceimpl;

import cn.usian.mapper.OrderMapper;
import cn.usian.pojo.Order;
import cn.usian.pojo.User;
import cn.usian.service.OrderService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.loadbalancer.IRule;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private IRule iRule;

    @Override
    public Order findById(Long id) {
        Order order = orderMapper.selectById(id);
        String url="http://userservice/user/"+order.getUserId();
        User user = restTemplate.getForObject(url, User.class);
        order.setUser(user);
        return order;
    }

    @Override
    public Page<Order> findPage(Integer pageIndex, Map pojo) {
        Page<Order> page = new Page<>(pageIndex,3);
        List<Order>list=orderMapper.findPage(pojo);
        page.setRecords(list);
        System.out.println("一共"+page.getTotal());
        return page;
    }

    @Override
    public void add(Order order) {
        orderMapper.insert(order);
    }

    @Override
    public void del(Integer id) {
        orderMapper.deleteById(id);
    }
}
